package com.soft.knowledge.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.soft.knowledge.entity.Category;
import com.soft.knowledge.entity.CategoryExample;
import com.soft.knowledge.mapper.CategoryMapper;
import com.soft.knowledge.req.CategoryQueryReq;
import com.soft.knowledge.req.CategorySaveReq;
import com.soft.knowledge.resp.CategoryQueryResp;
import com.soft.knowledge.resp.PageResp;
import com.soft.knowledge.utils.CopyUtil;
import com.soft.knowledge.utils.SnowFlake;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 电子书(Category)表服务实现类
 *
 * @author makejava
 * @since 2022-11-19 13:17:14
 */
@Service
public class CategoryService {
    private static final Logger LOG = LoggerFactory.getLogger(CategoryService.class);
    @Resource
    private CategoryMapper categoryMapper;

    @Resource
    private SnowFlake snowFlake;
    /**
     * CategoryReq：请求参数列表
     * CategoryResp: 返回参数列表
     * 最好不要出现Category类
     * @param req
     * @return
     */
    public PageResp<CategoryQueryResp> list(CategoryQueryReq req){

        CategoryExample example = new CategoryExample();
        CategoryExample.Criteria criteria = example.createCriteria();
        //动态sql
        if (!ObjectUtils.isEmpty(req.getName())){
            criteria.andNameLike("%"+req.getName()+"%");
        }
        //PageHelper和查询的list放到一起
        PageHelper.startPage(req.getPage(),req.getSize());
        List<Category> categoryList = categoryMapper.selectByExample(example);
        PageInfo<Category> pageInfo = new PageInfo<>(categoryList);
        LOG.info("总条数：{}", pageInfo.getTotal());
        LOG.info("总页数：{}", pageInfo.getPages());
//        List<CategoryResp> respList = new ArrayList<>();
//        for(Category category: categoryList){
////            CategoryResp resp = new CategoryResp();
////            BeanUtils.copyProperties(category, resp);
//            respList.add( CopyUtil.copy(category, CategoryResp.class));
//        }

        PageResp<CategoryQueryResp> pageResp = new PageResp<>();
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setList(CopyUtil.copyList(categoryList, CategoryQueryResp.class));
        return pageResp;
    }
    public List<Category> all(){

        CategoryExample example = new CategoryExample();

        List<Category> categoryList = categoryMapper.selectByExample(example);

        return categoryList;
    }
    public void save(CategorySaveReq req){
        Category category = CopyUtil.copy(req, Category.class);
        if(ObjectUtils.isEmpty(category.getId())){
            //新增
            category.setId(snowFlake.nextId());
            categoryMapper.insert(category);
        }else{
            //更新
            categoryMapper.updateByPrimaryKey(category);
        }
    }

    public void delete(Long id) {
        categoryMapper.deleteByPrimaryKey(id);
    }


}